package com.woniu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniu.been.entity.CategoryNode;
import com.woniu.been.entity.CourseCategories;
import com.woniu.been.entity.CourseChapters;
import com.woniu.been.entity.Courses;
import com.woniu.been.entity.Video;
import com.woniu.mapper.CourseChaptersMapper;
import com.woniu.service.CourseChaptersService;
import com.woniu.tools.CourseTreeBuilder;

import jakarta.transaction.Transactional;


@Service
public class CourseChaptersImpl implements CourseChaptersService {
    @Autowired
    CourseChaptersMapper courseChaptersMapper;

    @Override
//    public List<Courses> getCapter(CourseCategories coursecategories) {
    public List<CategoryNode>   getCapter(CourseCategories coursecategories) {
        List<Courses> course =  courseChaptersMapper.getCapter(coursecategories);
        List<CategoryNode>  res = CourseTreeBuilder.buildCourseTree(course);
        return  res;
    }

    @Override
    public List<Courses> getChapterById(Courses course) {
        List<Courses> coursse =  courseChaptersMapper.getCapterByID(course);
        return  coursse;
    }

    // 根据id查到相应的vedio
    @Override
    public List<Video> getVideos(Video video) {
        List<Video> videos =  courseChaptersMapper.getVideos(video);
        return videos;
    }
//    public List<Courses> getCapter() {
//        return courseChaptersMapper.getCapter();
//    }

    @Override
    public String getChapterNameById(Integer chapterId) {
        return courseChaptersMapper.findChapterNameById(chapterId);
    }
    @Override
    public List<CourseChapters> getChaptersByCourseId(Integer courseId) {
        return courseChaptersMapper.findChaptersByCourseId(courseId);
    }
    @Override
    @Transactional
    public void addChapter(CourseChapters chapter, Integer categoryId) {
        System.out.println("=== 开始新增章节 ===");
        System.out.println("传入的章节对象: " + chapter);
        System.out.println("传入的课程小类ID: " + categoryId);
        
        try {
            // 插入章节
            System.out.println("开始插入章节到数据库...");
            int insertResult = courseChaptersMapper.insertChapter(chapter);
            System.out.println("章节插入结果: " + insertResult);
            System.out.println("插入后的章节ID: " + chapter.getChapterId());
            
            // 插入章节与课程小类的关联关系
            System.out.println("开始插入章节-课程小类关联关系...");
            int categoryRelationResult = courseChaptersMapper.insertChapterCategoryRelation(chapter.getChapterId(), categoryId);
            System.out.println("章节-课程小类关联关系插入结果: " + categoryRelationResult);
            
            System.out.println("=== 章节新增成功 ===");
        } catch (Exception e) {
            System.err.println("=== 章节新增失败 ===");
            System.err.println("错误信息: " + e.getMessage());
            e.printStackTrace();
            throw e; // 重新抛出异常，让事务回滚
        }
    }
    @Override
    public void updateChapter(CourseChapters chapter) {
        courseChaptersMapper.updateChapter(chapter);
    }
    @Override
    @Transactional
    public void deleteChapter(Integer id) {
        // 删除章节与课程小类的关联关系
        courseChaptersMapper.deleteChapterCategoryRelationByChapterId(id);
        // 删除章节与课程的关联关系
        courseChaptersMapper.deleteRelationByChapterId(id);
        // 删除章节本身
        courseChaptersMapper.deleteChapter(id);
    }
    
    @Override
    public List<CourseChapters> getChaptersBySubCategoryId(Integer categoryId) {
        return courseChaptersMapper.findChaptersBySubCategoryId(categoryId);
    }

    @Override
    public Integer findChapterIdByName(String chapterName) {
        return courseChaptersMapper.findChapterIdByName(chapterName);
    }

}
